package com.controller.admin;

import com.controller.BaseController;
import com.entity.GoodsCatalog;
import com.entity.News;
import com.github.pagehelper.PageInfo;
import com.service.imple.home.NewsServiceImple;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新闻资讯
 */

@Controller
@RequestMapping("admin/news")
public class AdminNewsController extends BaseController<NewsServiceImple> {

    @RequestMapping("toNews")
    public String toNews(){
        return "center/admin/news/News";
    }

    @RequestMapping("News")
    public String goodsManage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum, ModelMap modelMap,String name){
        Map<String,Object> map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("newsTitle",name);
        PageInfo<News> pageInfo = BaseService.selectByPage(map);
        modelMap.put("pageInfo",pageInfo);
        return "center/admin/news/NewsPage";
    }

    @RequestMapping("delete")
    public String delete(Integer id){
        BaseService.delete(id);
        return "center/admin/news/News";
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Integer id,ModelMap modelMap){
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        List<News> list = BaseService.select(map);
        modelMap.put("list",list);
        return "center/admin/news/index";
    }

    @RequestMapping("Update")
    @ResponseBody
    public Map<String, Object> Update(News news){
        Map<String,Object> map = new HashMap<>();
        map.put("id",news.getId());
        List<News> list = BaseService.select(map);
        if (news.getNewsImg()==null){
            list.get(0).setNewsImg(list.get(0).getNewsImg());
        }else {
            list.get(0).setNewsImg(news.getNewsImg());
        }
        int count = BaseService.update(list.get(0));
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

    @RequestMapping("toFile")
    @ResponseBody
    public Map<String, Object> toFile( @RequestParam("fileImg")MultipartFile file, Integer uid, HttpSession session) throws IOException {

        //获取上传文件的名字
        String filename = file.getOriginalFilename();
        String newFileName = new Date().getTime()+filename.substring(filename.lastIndexOf("."));
        //上传部署后的目录中getRealPath
        String basePath = session.getServletContext().getRealPath("/public/images/");
        File baseFile = new File(basePath);
        if (!baseFile.exists()){//判断文件夹是否存在
            baseFile.mkdir();//创建文件夹
        }
        String savePath = basePath+newFileName;
        System.out.println(savePath);
        file.transferTo(new File(basePath+newFileName));

        Map<String,Object> map = new HashMap<>();
        map.put("id",uid);
        List<News> list= BaseService.select(map);
        list.get(0).setNewsImg("/"+newFileName);
        BaseService.update(list.get(0));

        map.put("url","http://localhost:8080/public/images/"+newFileName);
        map.put("url1","/"+newFileName);
        return map;
    }

    @RequestMapping("toAdd")
    public String toAdd(){
        return "center/admin/news/addNews";
    }

    @RequestMapping("add")
    @ResponseBody
    public Map<String, Object> add(News news ) {
        int  count = BaseService.insert(news);
        Map<String,Object> map = new HashMap<>();
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

}
